package hch.redesFerroviarias;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintStream;
import java.net.ServerSocket;
import java.net.Socket;

public class ServidorIDs {
	public static void main(String[] args) {
		ServerSocket serverSocketIDs;
		try {
			serverSocketIDs = new ServerSocket(1345);
			int estacionID = 1;
			int trenesID = 1;
			int indiceSentido = 0;
			
			while(true){
				Socket serverSocID = serverSocketIDs.accept();
				BufferedReader serverInID = new BufferedReader(new InputStreamReader(serverSocID.getInputStream()));
				PrintStream serverOutID = new PrintStream(serverSocID.getOutputStream());
				
				//IDS DE ESTACIONES
				int cantidadDeIDsEstacion = serverInID.read();
				for(int i=0;i<cantidadDeIDsEstacion;i++){
					serverOutID.write(estacionID);
					estacionID++;
				}
				
				//IDS DE TRENES
				int cantidadDeIDsTrenes = serverInID.read();
				for(int j=0;j<cantidadDeIDsTrenes;j++){
					serverOutID.write(trenesID);
					if(indiceSentido==2)
						indiceSentido=0;
					serverOutID.write(indiceSentido);
					indiceSentido++;
					trenesID++;
				}
			}
		}
		catch (IOException e) {
			System.out.println("Could not listen on port: 1234");
			System.exit(-1);
			}
		}

}
